package terrain;

import java.util.logging.Logger;

import test.Constants;

import com.jme3.app.Application;
import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.texture.Texture;
import com.jme3.util.TangentBinormalGenerator;

public class TopDown {
	private static final Logger logger = Logger.getLogger(Application.class
			.getName());
	
	private static final String simpleTex = "Common/MatDefs/Misc/SimpleTextured.j3md";
	//private static final String earthTex = "texture/earth-top.png";
	private static final String stoneTex = "texture/stone-top.png";
	private static final String mudTex = "texture/mud.png";
	private static final String stoneplatesTex = "texture/stoneplates.png";
	private static final String waterTex = "texture/water.png";
	private static final String stonewallTex = "texture/stonewall-top.png";
	
	private static final String earthTex = "texture/earth2-top.png";
	private static final String earthNormalTex = "texture/earth2-top-normal.png";
	
	private Geometry earthTop;
	private Geometry stoneTop;
	private Geometry wallTop;
	private Geometry mudDown;
	private Geometry stoneDown;
	private Geometry waterDown;
	
	public TopDown(AssetManager assetManager) {
		Box b = new Box(Vector3f.ZERO, 0.5f, 0.5f, 0);
		
		//Generic Top/Down 
		Geometry genericDown = new Geometry("Down", b);
		genericDown.move(0.5f, 0.5f, 0);
		
		Geometry genericTop = genericDown.clone();
		genericTop.setName("Top");
		genericTop.move(0, 0, 2);
		
		Material matEarth = new Material(assetManager, simpleTex);
		Texture texEarth = assetManager.loadTexture(earthTex);
		matEarth.setTexture("m_ColorMap", texEarth);
		earthTop = genericTop.clone();
		earthTop.setName("Earth");
		//earthTop.setMaterial(matEarth);
		
		/** A bumpy rock with a shiny light effect */
	    TangentBinormalGenerator.generate(earthTop);           // for lighting effect
	    Material mat_lit = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
	    mat_lit.setTexture("m_DiffuseMap", assetManager.loadTexture(earthTex));
	    mat_lit.setTexture("m_NormalMap", assetManager.loadTexture(earthNormalTex));
	    mat_lit.setFloat("m_Shininess", 5f); // [0,128]
	    earthTop.setMaterial(mat_lit);

		
		
		Material matStone = new Material(assetManager, simpleTex);
		Texture texStone = assetManager.loadTexture(stoneTex);
		matStone.setTexture("m_ColorMap", texStone);
		stoneTop = genericTop.clone();
		stoneTop.setName("Stone");
		stoneTop.setMaterial(matStone);
		
		Material matWall = new Material(assetManager, simpleTex);
		Texture texWall = assetManager.loadTexture(stonewallTex);
		matWall.setTexture("m_ColorMap", texWall);
		wallTop = genericTop.clone();
		wallTop.setName("Stone Wall");
		wallTop.setMaterial(matWall);
		
		Material matMud = new Material(assetManager, simpleTex);
		Texture texMud = assetManager.loadTexture(mudTex);
		matMud.setTexture("m_ColorMap", texMud);
		mudDown = genericDown.clone();
		mudDown.setName("Mud");
		mudDown.setMaterial(matMud);

		Material matPlates = new Material(assetManager, simpleTex);
		Texture texPlates = assetManager.loadTexture(stoneplatesTex);
		matPlates.setTexture("m_ColorMap", texPlates);
		stoneDown = genericDown.clone();
		stoneDown.setName("Stoneplates");
		stoneDown.setMaterial(matPlates);

		Material matWater = new Material(assetManager, simpleTex);
		Texture texWater = assetManager.loadTexture(waterTex);
		matWater.setTexture("m_ColorMap", texWater);
		waterDown = genericDown.clone();
		waterDown.setName("Water");
		waterDown.setMaterial(matWater);
	}
	
	public Geometry getTop(int type) {
		if (type == Constants.EARTH) {
			return earthTop.clone();
		} else if (type == Constants.STONE) {
			return stoneTop.clone();
		} else if (type == Constants.STONEWALL) {
			return wallTop.clone();
		}
		logger.warning("wrong top type transmitted: " + type);
		return null;
	}

	public Geometry getDown(int type) {
		if (type == Constants.MUD) {
			return mudDown.clone();
		} else if (type == Constants.STONEPLATES) {
			return stoneDown.clone();
		} else if (type == Constants.WATER) {
			return waterDown.clone();
		}
		logger.warning("wrong down type transmitted: " + type);
		return null;
	}
}
